<template>
  <div>
    <div class="flex justify-between items-center pr-4 py-2 bg-white">
      <LayoutBreadcrumb class="grow" />
      <!--      <span>-->
      <!--        <a-input v-model:value="keyword" style="width: 220px" placeholder="请输入检测关键字" />-->
      <!--        <a-button type="primary">查询</a-button>-->
      <!--      </span>-->
    </div>
    <div class="mt-2 bg-white p-2">
      <this-search ref="search" @project-change="projectChange" @action="action"/>
    </div>
    <div class="mt-2 px-4 pb-4 bg-white">
      <this-table ref="table"/>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent,onMounted } from 'vue';
import { LayoutBreadcrumb } from '../../../layouts/default/header/components';
import { ref } from 'vue';
import thisSearch from './search.vue';
import thisTable from './table.vue';
import { defHttp } from '/@/utils/http/axios';

interface Project {
  id: number;
  name: string;
}
export default defineComponent({
  components: { LayoutBreadcrumb, thisSearch, thisTable },
  setup() {
    const keyword = ref('');
    const projects = ref<[Project]>();
    const total = ref<number>(0);
    const search = ref();
    const table = ref();

    const getProjects = ()=> {
      defHttp
        .get({
          url: '/project/search',
          params: { pageIndex: 1, pageSize: 500, keyword: '' },
        })
        .then((res) => {
          projects.value = res.data.pageList;
          search.value.setProjects(projects.value );
          table.value.setProjects(projects.value);
        });
    }

    const projectChange = (projectId:number)=>{
      search.value.setDtu(projectId);
      defHttp
        .get({
          url: '/sensor/get-by-project-id',
          params: { projectId:projectId },
        })
        .then((res) => {
          table.value.setSensorMap(res.data? res.data:[]);
        });
    }

    const action = (p:any)=>{
      defHttp
        .post({
          url: '/warn/get-page',
          params: p,
        })
        .then((res) => {
          total.value = res.data.total;
          table.value.setData(res.data.pageList,{current:p.pageIndex,pageSize:p.pageSize,total:total.value} );
        });
    }

    onMounted(() => {
      getProjects();
    });

    return {
      keyword,
      search,
      table,
      getProjects,
      projectChange,
      action
    };
  },
});
</script>
